package com.dudu.dao;

import java.sql.Connection;
import java.util.List;

import net.sf.json.JSONObject;

import com.dudu.model.RoleMenu;

import core.CoreDao;
import core.CoreDaoInvoke;
import core.db.DBConnectionUtil;

public class RoleMenuDao {

	public static void main(String[] args) {

		CoreDao coreDao = new CoreDaoInvoke();
		RoleMenuDao dao = new RoleMenuDao();


		RoleMenu bean = new RoleMenu();
		bean.setRole_id(3);
		for(int i=6;i<10;i++){
			bean.setMenu_id(i);
			
			dao.insert(coreDao, bean);
		}
	}

	public int insert(CoreDao dao, RoleMenu bean) {

		Connection con = null;
		int result = -1;
		try {
			con = DBConnectionUtil.getDBConnection("emate1");
			StringBuffer sql = new StringBuffer(
					"INSERT INTO t_role_menu(role_id, menu_id)");
			sql.append("VALUES(?, ?)");

			List<JSONObject> list = dao.serviceJsonSql(
					con,
					sql.toString(),
					new Object[] { 
						bean.getRole_id(), bean.getMenu_id()
					});

			JSONObject obj = list.get(0);
			result = obj.optInt("result");

		} catch (Exception ex) {
			ex.printStackTrace();
		} finally {
			DBConnectionUtil.close(null, null, con);
		}
		return result;
	}
	
	public List<RoleMenu> queryListByRoleId(CoreDao dao, int role_id) {

		Connection con = null;
		List<RoleMenu> result = null;

		try {
			con = DBConnectionUtil.getDBConnection("emate1");

			StringBuffer sql = new StringBuffer(
					"select id, role_id, menu_id FROM t_role_menu where role_id=?");
			List<JSONObject> list = dao.serviceJsonSql(con, sql.toString(),
					new Object[] { role_id });
			if (list != null && list.size() > 0) {
				result = dao.covertToBean(list, RoleMenu.class);
			}

		} catch (Exception ex) {
			ex.printStackTrace();
		} finally {
			DBConnectionUtil.close(null, null, con);
		}
		return result;
	}
	
	public List<JSONObject> queryMenuByRole(CoreDao dao, int roleid) {

		Connection con = null;
		List<JSONObject> result = null;
		
		try {
			con = DBConnectionUtil.getDBConnection("emate1");
			StringBuffer sql = new StringBuffer(
					" SELECT m.*,IFNULL((SELECT r.id FROM t_role_menu r WHERE r.menu_id=m.id AND r.role_id=?), -1)AS check_status " +
					" FROM t_menu m order by m.sort asc ");
			result = dao.serviceJsonSql(con, sql.toString(),
					new Object[] { roleid });

		} catch (Exception ex) {
			ex.printStackTrace();
		} finally {
			DBConnectionUtil.close(null, null, con);
		}
		return result;
	}
	
	public int deleteByRoleid(CoreDao dao, int roleid) {

		Connection con = null;
		int result = -1;
		try {
			con = DBConnectionUtil.getDBConnection("emate1");
			
			StringBuffer sql = new StringBuffer(" delete from t_role_menu where role_id =? ");
			List<JSONObject> list = dao.serviceJsonSql(con, sql.toString(),
					new Object[] { roleid });

			if (list != null && list.size() > 0) {
				list.get(0);
				JSONObject obj = list.get(0);
				result = obj.optInt("result");
			}

		} catch (Exception ex) {
			ex.printStackTrace();
		} finally {
			DBConnectionUtil.close(null, null, con);
		}
		return result;
	}
}
